package cn.butcher21.dao;

import cn.butcher21.pojo.User;
import cn.butcher21.utils.DBUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * butcher create in 2021/04/24/13:19
 * <p>
 * 用户操作
 */
public class UserDao {

    public List<User> selectUserS() throws SQLException, IOException, ClassNotFoundException {
        List<User> list = new ArrayList<User>();
        DBUtil dbUtil = new DBUtil();
        String sql = "SELECT U_NAME,U_PASSWORD FROM T_USER";
        PreparedStatement ps = dbUtil.getPs(sql);
        ResultSet rs = dbUtil.getRs(ps.executeQuery());
        while (rs.next()) {
            User user = new User();
            user.setName(rs.getString(1));
            user.setPassword(rs.getString(2));
            list.add(user);
        }
        return list;
    }


    public boolean checkUser(String name, String password) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "SELECT * FROM T_USER WHERE u_name = ? AND u_password=?";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(1, name == null ? "" : name);
        ps.setString(2, password == null ? "" : password);
        ResultSet rs = dbUtil.getRs(ps.executeQuery());
        if (rs.next()) {
            return true;
        }
        return false;
    }

    /**
     * 添加一个用户
     *
     * @param name     用户名
     * @param password 密码
     * @return 状态
     * @throws SQLException           异常
     * @throws IOException            异常
     * @throws ClassNotFoundException 异常
     */
    public boolean add(String name, String password) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "INSERT INTO T_USER(U_NAME,U_PASSWORD) VALUES(?,?)";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(1, name);
        ps.setString(2, password);
        return ps.execute();
    }

    /**
     * 修改密码
     *
     * @param name     用户名
     * @param password 密码
     * @return 状态
     * @throws SQLException           异常
     * @throws IOException            异常
     * @throws ClassNotFoundException 异常
     */
    public boolean update(String name, String password) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "UPDATE T_USER SET U_PASSWORD=? where U_NAME=?";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(2, name);
        ps.setString(1, password);
        return ps.execute();
    }

    /**
     * 更新用户名和密码
     *
     * @param oldName  旧的用户名
     * @param newName  新的用户名
     * @param password 新的密码
     * @return 状态
     * @throws SQLException           异常
     * @throws IOException            异常
     * @throws ClassNotFoundException 异常
     */
    public boolean update(String oldName, String newName, String password) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "UPDATE T_USER SET U_NAME=?,U_PASSWORD=? where U_NAME=?";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(1, newName);
        ps.setString(2, password);
        ps.setString(3, oldName);
        return ps.execute();
    }

    /**
     * 删除一个用户
     *
     * @param name 用户名
     * @return 状态
     * @throws SQLException           异常
     * @throws IOException            异常
     * @throws ClassNotFoundException 异常
     */
    public boolean delete(String name) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "DELETE FROM T_USER WHERE U_NAME=?";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(1, name);
        return ps.execute();
    }

    public boolean checkUserExist(String name) throws SQLException, IOException, ClassNotFoundException {
        DBUtil dbUtil = new DBUtil();
        String sql = "SELECT U_NAME FROM T_USER WHERE U_NAME=? ";
        PreparedStatement ps = dbUtil.getPs(sql);
        ps.setString(1, name);
        ResultSet rs = dbUtil.getRs(ps.executeQuery());
        if (rs.next()) return true;
        return false;
    }
}
